﻿using Comm.Helper;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Kinlo.Model;
using Kinlo.Repo;
using Kinlo.UI.Main.View;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using Comm.CommHelper;
using Comm;

namespace Kinlo.UI.Main.VM
{
    public class QueryVM : ViewModelBase
    {
        QueryView View;
        BeforWeightFirstRep BWDB = new BeforWeightFirstRep();
        AfterWeightFirstRep AFDB = new AfterWeightFirstRep();
        CodeFirstRep CFDB = new CodeFirstRep();

        public QueryVM(QueryView view)
        {
            this.View = view;
            AfterWeights = new ObservableCollection<AfterWeightFirst>();
            CodeFirsts = new ObservableCollection<CodeFirst>();
            StartTime3 = StartTime1 = StartTime = DateTime.Now.AddDays(-1);
            EndTime3 = EndTime1 = EndTime = DateTime.Now;


            var obj1 = new ComboValue { Name = "所有", Value = "%" };
            var obj2 = new ComboValue { Name = "合格", Value = "%True%" };
            var obj3 = new ComboValue { Name = "不合格", Value = "%False%" };
            ResultType = new ObservableCollection<ComboValue>();
            ResultType.Add(obj1);
            ResultType.Add(obj2);
            ResultType.Add(obj3);
            ResultTypeSelected = obj1;

            ResultType1 = new ObservableCollection<ComboValue>();
            ResultType1.Add(obj1);
            ResultType1.Add(obj2);
            ResultType1.Add(obj3);
            ResultTypeSelected1 = obj1;
        }
        #region 后称重
        DateTime startTime;
        /// <summary>
        /// 开始时间
        /// </summary>
        public DateTime StartTime
        {
            get { return startTime; }
            set
            {
                startTime = value;
                RaisePropertyChanged(() => StartTime);
            }
        }
        DateTime endTime;
        /// <summary>
        /// 结束时间
        /// </summary>
        public DateTime EndTime
        {
            get { return endTime; }
            set
            {
                endTime = value;
                RaisePropertyChanged(() => EndTime);
            }
        }
        string queryBarcode;
        /// <summary>
        /// 结束时间
        /// </summary>
        public string QueryBarcode
        {
            get { return queryBarcode; }
            set
            {
                queryBarcode = value.Trim();
                RaisePropertyChanged(() => QueryBarcode);
            }
        }

        ComboValue resultTypeSelected;
        public ComboValue ResultTypeSelected
        {
            get { return resultTypeSelected; }
            set
            {
                resultTypeSelected = value;
                RaisePropertyChanged(() => ResultTypeSelected);
            }
        }

        ObservableCollection<AfterWeightFirst> afterWeights;
        ObservableCollection<ReWork> reWorkDatas;

        /// <summary>
        /// 查询结果
        /// </summary>
        public ObservableCollection<AfterWeightFirst> AfterWeights
        {
            get { return afterWeights; }
            set
            {
                afterWeights = value;
                RaisePropertyChanged(() => AfterWeights);
            }
        }
        public ObservableCollection<ReWork> ReWorkDatas
        {
            get { return reWorkDatas; }
            set
            {
                reWorkDatas = value;
                RaisePropertyChanged(() => ReWorkDatas);
            }
        }

        ObservableCollection<ComboValue> resultType;
        /// <summary>
        /// 结果类型
        /// </summary>
        public ObservableCollection<ComboValue> ResultType
        {
            get { return resultType; }
            set
            {
                resultType = value;
                RaisePropertyChanged(() => ResultType);
            }
        }


        RelayCommand cmdQuery;
        /// <summary>
        /// 
        /// </summary>
        public RelayCommand CmdQuery
        {
            get { return cmdQuery ?? (cmdQuery = new RelayCommand(() => DoCmdQuery())); }
        }
        void DoCmdQuery()
        {
            var query = new QueryClass();
            query.time1 = StartTime.ToString("yyyy-MM-dd HH:mm:ss");
            query.time2 = EndTime.ToString("yyyy-MM-dd HH:mm:ss");
            query.code = QueryBarcode;
            query.okngStatus = ResultTypeSelected.Value;

            Task.Factory.StartNew(new Action<object>((p) =>
            {
                var q = (QueryClass)p;
                var data = AFDB.GetAfterFirst(q.time1, q.time2, q.code, q.okngStatus);
                if (data != null)
                {
                    AfterWeights = new ObservableCollection<AfterWeightFirst>(data);
                }
                else
                {
                    MessageBox.Show("没查到相应的结果", "提示");
                }
            }), query);
        }
        string Locate = "ZH";

        RelayCommand cmdExport;
        /// <summary>
        ///  
        /// </summary>
        public RelayCommand CmdExport
        {
            get { return cmdExport ?? (cmdExport = new RelayCommand(() => DoCmdExport())); }
        }
        void DoCmdExport()
        {
            try
            {
                SaveFileDialog dlg = new SaveFileDialog();
                dlg.Filter = "Excel 工作簿(*.xls)|*.xls";
                dlg.FileName = DateTime.Now.ToString("yyyy-MM-dd HH点mm分ss秒");
                if (dlg.ShowDialog() == true)
                {
                    var query = new QueryClass();
                    query.time1 = StartTime.ToString("yyyy-MM-dd HH:mm:ss");
                    query.time2 = EndTime.ToString("yyyy-MM-dd HH:mm:ss");
                    query.code = QueryBarcode;
                    query.okngStatus = ResultTypeSelected.Value;

                    var data = AFDB.GetAfterFirst(query.time1, query.time2, query.code, query.okngStatus);

                    if (data != null)
                    {
                        var datas = new ObservableCollection<AfterWeightFirst>(data);
                        var result = false;
                        if (GlobalValues.Locate.ToUpper() == "ZH")
                        {
                            result = ExcelHelper.SaveToExcel(dlg.FileName, datas.ToAfterWeightFirst2());
                        }
                        else
                        {
                            result = ExcelHelper.SaveToExcel(dlg.FileName, datas);
                        }
                        if (result)
                        {
                            MessageBox.Show("保存成功");
                        }
                    }
                    else
                    {
                        MessageBox.Show("没有查到相应的数据", "提示");
                    }

                }
            }
            catch (IOException ioex)
            {
                MessageBox.Show(ioex.Message);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误");
            }
        }
        #endregion




        #region 短路

        DateTime startTime1;
        /// <summary>
        /// 开始时间
        /// </summary>
        public DateTime StartTime1
        {
            get { return startTime1; }
            set
            {
                startTime1 = value;
                RaisePropertyChanged(() => StartTime1);
            }
        }
        DateTime endTime1;
        /// <summary>
        /// 结束时间
        /// </summary>
        public DateTime EndTime1
        {
            get { return endTime1; }
            set
            {
                endTime1 = value;
                RaisePropertyChanged(() => EndTime1);
            }
        }
        string queryBarcode1;
        /// <summary>
        /// 结束时间
        /// </summary>
        public string QueryBarcode1
        {
            get { return queryBarcode1; }
            set
            {
                queryBarcode1 = value.Trim();
                RaisePropertyChanged(() => QueryBarcode1);
            }
        }

        ComboValue resultTypeSelected1;
        public ComboValue ResultTypeSelected1
        {
            get { return resultTypeSelected1; }
            set
            {
                resultTypeSelected1 = value;
                RaisePropertyChanged(() => ResultTypeSelected1);
            }
        }
        ObservableCollection<ComboValue> resultType1;
        /// <summary>
        /// 结果类型
        /// </summary>
        public ObservableCollection<ComboValue> ResultType1
        {
            get { return resultType1; }
            set
            {
                resultType1 = value;
                RaisePropertyChanged(() => ResultType1);
            }
        }
        ObservableCollection<CodeFirst> codeFirsts;
        /// <summary>
        /// 短路查询结果
        /// </summary>
        public ObservableCollection<CodeFirst> CodeFirsts
        {
            get { return codeFirsts; }
            set
            {
                codeFirsts = value;
                RaisePropertyChanged(() => CodeFirsts);
            }
        }

        RelayCommand cmdQuery1;
        /// <summary>
        /// 查询 
        /// </summary>
        public RelayCommand CmdQuery1
        {
            get { return cmdQuery1 ?? (cmdQuery1 = new RelayCommand(() => DoCmdQuery1())); }
        }
        void DoCmdQuery1()
        {
            var query = new QueryClass();
            query.time1 = StartTime1.ToString("yyyy-MM-dd HH:mm:ss");
            query.time2 = EndTime1.ToString("yyyy-MM-dd HH:mm:ss");
            query.code = QueryBarcode1;
            query.okngStatus = ResultTypeSelected1.Value;

            Task.Factory.StartNew(new Action<object>((p) =>
            {
                var q = (QueryClass)p;

                var data = CFDB.GetCodeFirst(q.time1, q.time2, q.code, q.okngStatus);
                if (data != null)
                {
                    CodeFirsts = new ObservableCollection<CodeFirst>(data);
                }
                else
                {
                    MessageBox.Show("没查到相应的结果", "提示");
                }
            }), query);
        }

        RelayCommand cmdExport1;
        /// <summary>
        ///  
        /// </summary>
        public RelayCommand CmdExport1
        {
            get { return cmdExport1 ?? (cmdExport1 = new RelayCommand(() => DoCmdExport1())); }
        }
        void DoCmdExport1()
        {
            try
            {
                SaveFileDialog dlg = new SaveFileDialog();
                dlg.Filter = "Excel 工作簿(*.xls)|*.xls";
                dlg.FileName = DateTime.Now.ToString("yyyy-MM-dd HH点mm分ss秒");
                if (dlg.ShowDialog() == true)
                {
                    var query = new QueryClass();
                    query.time1 = StartTime1.ToString("yyyy-MM-dd HH:mm:ss");
                    query.time2 = EndTime1.ToString("yyyy-MM-dd HH:mm:ss");
                    query.code = QueryBarcode1;
                    query.okngStatus = ResultTypeSelected1.Value;
                    var data = CFDB.GetCodeFirst(query.time1, query.time2, query.code, query.okngStatus);
                    if (data != null)
                    {
                        var datas = new ObservableCollection<CodeFirst>(data);
                        var result = ExcelHelper.SaveToExcel(dlg.FileName, datas);
                        if (result)
                        {
                            MessageBox.Show("保存成功");
                        }
                    }
                    else
                    {
                        MessageBox.Show("没有查到相应的数据", "提示");
                    }
                }
            }
            catch (IOException ioex)
            {
                MessageBox.Show(ioex.Message);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误");
            }
        }
        #endregion


        #region 返工
        ComboValue resultTypeSelected3;
        public ComboValue ResultTypeSelected3
        {
            get { return resultTypeSelected3; }
            set
            {
                resultTypeSelected3 = value;
                RaisePropertyChanged(() => ResultTypeSelected3);
            }
        }
        DateTime startTime3;
        /// <summary>
        /// 开始时间
        /// </summary>
        public DateTime StartTime3
        {
            get { return startTime3; }
            set
            {
                startTime3 = value;
                RaisePropertyChanged(() => StartTime3);
            }
        }
        DateTime endTime3;
        /// <summary>
        /// 结束时间
        /// </summary>
        public DateTime EndTime3
        {
            get { return endTime3; }
            set
            {
                endTime3 = value;
                RaisePropertyChanged(() => EndTime3);
            }
        }
        string queryBarcode3;
        /// <summary>
        /// 结束时间
        /// </summary>
        public string QueryBarcode3
        {
            get { return queryBarcode3; }
            set
            {
                queryBarcode3 = value.Trim();
                RaisePropertyChanged(() => QueryBarcode3);
            }
        }

        RelayCommand cmdQuery3;
        /// <summary>
        /// 
        /// </summary>
        public RelayCommand CmdQuery3
        {
            get { return cmdQuery3 ?? (cmdQuery3 = new RelayCommand(() => DoCmdQuery3())); }
        }
        void DoCmdQuery3()
        {
            if (ResultTypeSelected3 == null)
            {
                MessageBox.Show("请选择结果类型", "错误");
                return;
            }
            var query = new QueryClass();
            query.time1 = StartTime3.ToString("yyyy-MM-dd HH:mm:ss");
            query.time2 = EndTime3.ToString("yyyy-MM-dd HH:mm:ss");
            query.code = QueryBarcode3;
            query.okngStatus = ResultTypeSelected3.Value;

            Task.Factory.StartNew(new Action<object>((p) =>
            {
                var q = (QueryClass)p;
                var data = CFDB.GetReWorks(q.time1, q.time2, q.code, q.okngStatus);
                if (data != null)
                {
                    ReWorkDatas = new ObservableCollection<ReWork>(data);
                }
                else
                {
                    MessageBox.Show("没查到相应的结果", "提示");
                }
            }), query);
        }

        RelayCommand cmdExport3;
        /// <summary>
        ///  
        /// </summary>
        public RelayCommand CmdExport3
        {
            get { return cmdExport3 ?? (cmdExport3 = new RelayCommand(() => DoCmdExport3())); }
        }
        void DoCmdExport3()
        {
            try
            {
                SaveFileDialog dlg = new SaveFileDialog();
                dlg.Filter = "Excel 工作簿(*.xls)|*.xls";
                dlg.FileName = DateTime.Now.ToString("yyyy-MM-dd HH点mm分ss秒");
                if (dlg.ShowDialog() == true)
                {
                    if (ReWorkDatas != null && ReWorkDatas.Count > 0)
                    {
                        var result = ExcelHelper.SaveToExcel(dlg.FileName, ReWorkDatas);
                        if (result)
                        {
                            MessageBox.Show("保存成功");
                        }
                    }
                    else
                    {
                        MessageBox.Show("没有查到相应的数据", "提示");
                    }
                }
            }
            catch (IOException ioex)
            {
                MessageBox.Show(ioex.Message);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误");
            }
        }

        #endregion




        RelayCommand<DataGridRowEventArgs> cmdLoadingRow;
        public RelayCommand<DataGridRowEventArgs> CmdLoadingRow
        {
            get { return cmdLoadingRow ?? (cmdLoadingRow = new RelayCommand<DataGridRowEventArgs>((e) => DoCmdLoadingRow(e))); }
        }
        void DoCmdLoadingRow(DataGridRowEventArgs e)
        {
            e.Row.Header = e.Row.GetIndex() + 1;
        }


    }
}
